package com.dnake.misc;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import com.dnake.panel.BaseLabel;
import com.dnake.panel.WakeTask;
import com.dnake.utils.NetUtil;
import com.dnake.utils.PingUtil;
import com.dnake.v700.devent;
import com.dnake.v700.dmsg;
import com.dnake.v700.eDhcp;
import com.dnake.v700.sLocale;
import com.dnake.v700.sys;
import com.dnake.v700.utils;
import com.dnake.v700.vt_uart;

import java.util.LinkedList;
import java.util.Queue;
import java.util.logging.Logger;

@SuppressLint("HandlerLeak")
public class SysTalk extends Service {

	public static Context mContext = null;

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	public static Queue<String> Keys = new LinkedList<String>();

	private static Handler e_touch = null;
	private static Handler e_start = null;
	private static Handler e_play = null;
	public static boolean netState = false;
	@Override
	public void onCreate() {
		super.onCreate();

		mContext = this;

		dmsg.start("/ui");
		devent.setup();

		WakeTask.onCreate(this);

		sys.load();
		SysAccess.load();
		SysCard.load();
		SysForward.load();
		SysSpecial.load();
//		SysNameDial.load();

		vt_uart.start();
		vt_uart.setup(0, 3);

		SysElev.start();

		e_touch = new Handler() {
			private MediaPlayer mPlayer = null;

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);

				WakeTask.acquire();
				for(int i=0; i<3; i++) {
					if (WakeTask.isScreenOn())
						break;
					try {
						Thread.sleep(800);
					} catch (InterruptedException e) {
					}
				}
				if (mPlayer != null) {
					mPlayer.release();
					mPlayer = null;
				}
				OnCompletionListener listener = new OnCompletionListener() {
					public void onCompletion(MediaPlayer p) {
						p.reset();
						p.release();
						mPlayer = null;
					}
				};

				if (Sound.key_0_9) {
					String key = (String) msg.obj;
					int n = key.charAt(0)-'0';
					if (n >= 0 && n <= 9) {
						mPlayer = Sound.play(Sound.key[n], false, listener);
					} else
						mPlayer = Sound.play(Sound.press, false, listener);
				} else
					mPlayer = Sound.play(Sound.press, false, listener);

				tBroadcast();
			}
		};

//		e_start = new Handler() {
//			@Override
//			public void handleMessage(Message msg) {
//				super.handleMessage(msg);
//
//				if (TalkLabel.mIntent == null) {
//					WakeTask.acquire();
//					for(int i=0; i<3; i++) {
//						if (WakeTask.isScreenOn())
//							break;
//						try {
//							Thread.sleep(800);
//						} catch (InterruptedException e) {
//						}
//					}
//
//					dmsg req = new dmsg();
//					while (req.to("/media/rtsp/length", null) == 200) {
//						req.to("/media/rtsp/stop", null);
//						try {
//							Thread.sleep(200);
//						} catch (InterruptedException e) {
//						}
//					}
//
//					TalkLabel.mIntent = new Intent(SysTalk.this, TalkLabel.class);
//					TalkLabel.mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
//					startActivity(TalkLabel.mIntent);
//				}
//			}
//		};

//		e_play = new Handler() {
//			@Override
//			public void handleMessage(Message msg) {
//				super.handleMessage(msg);
//				if (TalkLabel.mContext != null)
//					TalkLabel.mContext.play();
//			}
//		};

		ProcessThread p = new ProcessThread();
		Thread t = new Thread(p);
		t.start();

		devent.boot = true;

		dmsg req = new dmsg();
		req.to("/talk/setid", null);

		sLocale.load();
		eDhcp.start();

		SysWifi.start(this);
		SysBT.start(this);
		SysAccess.setLock(0, 0); //����

		NetworkThread nt = new NetworkThread();
		Thread networkThread = new Thread(nt);
		networkThread.start();
//		if (!HomeActivity.mOK) {
//			MainActivity.mOK = true;
//			Intent it = new Intent(this, MainActivity.class);
//			it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
//			startActivity(it);
//		}
	}
	public class NetworkThread implements Runnable {
		@Override
		public void run() {
			while(true) {
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					Log.d("ww","ww");
				}
				if (NetUtil.getNetWorkState(mContext)){
					Float ping = PingUtil.getPacketLossFloat("www.baidu.com",3,5);
					if (ping!=-1&&ping!=100){
						netState = true;
					}else {
						netState = false;
					}
				}else {
					netState = false;
				}

			}
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		WakeTask.onDestroy();
	}

	public static class ProcessThread implements Runnable {
		@Override
		public void run() {
			try {
				Thread.sleep(2*1000);
			} catch (InterruptedException e) {
			}
			if (utils.getLocalIp() == null)
				utils.eth0_reset();

			while (true) {
				SysAccess.process();
				SysCard.process();
				SysWifi.process();
//				SysNameDial.process();

				try {
					Thread.sleep(200);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void start() {
		if (e_start != null)
			e_start.sendMessage(e_start.obtainMessage());
	}

	public static void play() {
		if (e_play != null)
			e_play.sendMessage(e_play.obtainMessage());
	}

	public static void touch(String key) {
		BaseLabel.mKeyTs = System.currentTimeMillis();
		if (e_touch != null) {
			Message m = e_touch.obtainMessage();
			m.obj = key;
			e_touch.sendMessage(m);
		}
	}

	public static class ipErr {
		public static int result = 0;
		public static String mac;
	}

	public static void ipMacErr(int result, String ip, String mac) {
		ipErr.result = result;
		ipErr.mac = mac;
		WakeTask.acquire();
	}

	public static void tBroadcast() {
		if (mContext != null) {
			Intent it = new Intent("com.dnake.broadcast");
			it.putExtra("event", "com.dnake.talk.touch");
			mContext.sendBroadcast(it);
		}
	}
}
